Steps to create route

  • STEP
    1. create route in route/web.php
    2. we can add multiple routes in the web.php file

    route/web.php

    1 add the route URL what you want

    2 map the controller and corresponding action

    
    
                        Route::get('students', [StudentController::class, 'index']);
    
                        
    When calling the route, we will get the error 'targetClass StudentController is not exists'. 1) Define the StudentController class in app/Http/Controllers
    2) include the StudentController in route/web.php file

    include the controller files in route/web.php file

    
                        use App\Http\Controllers\StudentController;
                        

    Complete code of routes/web.php

    
                        use Illuminate\Support\Facades\Route;
                        use App\Http\Controllers\StudentController;
    
                        Route::get('students', [StudentController::class, 'index']);
                        

    Define routes with namespaces

    we can also define route without import or use (include ) controller

    
                        # Defines all the routes that resides in App/Http/Controllers/StudentController folder
                        Route::get('students', [App\Http\Controllers\StudentController::class, 'index']  );
                        Route::get('students/{id}',  [App\Http\Controllers\StudentController::class, 'get'] );
    
                        

    Naming convention